package cate.game.role.x;

import cate.game.role.RolePart;
import com.fasterxml.jackson.core.JsonProcessingException;
import easy.java.codec.JsonCodec;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.Optional;

@Slf4j
public class DirectRolePartWorker<P extends RolePart> extends RolePartWorker<P> {
    @Override
    public Optional<P> findById(MongoRepository<P, String> repo, String uid) {
        return repo.findById(uid);
    }

    @Override
    public void save(MongoRepository<P, String> repo, P part) {
        try {
            repo.save(part);
        } catch (Throwable e) {
            try {
                log.error("首次保存{}失败，obj={}，e={}", part.getClass(), JsonCodec.instance().encodeString(part), e);
                Thread.sleep(50);
                repo.save(part);
            } catch (JsonProcessingException ex) {
                log.error("json序列化失败, ex=", ex);
            } catch (Exception exx) {
                log.error("二次保存也失败, exx=", exx);
            }
        }
    }
}
